Micron Document
`:top
The Java programming language's `F33f`_`[Java Collections Framework`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Java_Collections_Framework]`_`f version 1.5 and later defines and implements the original regular single-threaded Maps, and also new thread-safe Maps implementing the `!`B100`F9d9java.util.concurrent.ConcurrentMap`f`b`! interface among other concurrent interfaces.`:cite-ref-footnotegoetzpeierlsblochbowbeer200684-85-5-2-concurrent-collections-1-0[`F5bf`_`[1`#cite-note-footnotegoetzpeierlsblochbowbeer200684-85-5-2-concurrent-collections-1]`_`f] In Java 1.6, the `!`B100`F9d9java.util.NavigableMap`f`b`! interface was added, extending `!`B100`F9d9java.util.SortedMap`f`b`!, and the `!`B100`F9d9java.util.concurrent.ConcurrentNavigableMap`f`b`! interface was added as a subinterface combination.

>>Contents

• `F0af`_`[Java Map Interfaces`#java-map-interfaces]`_`f
• `F0af`_`[Implementations`#implementations]`_`f
• `F0af`_`[ConcurrentHashMap`#concurrenthashmap]`_`f
• `F0af`_`[ConcurrentSkipListMap`#concurrentskiplistmap]`_`f
• `F0af`_`[Ctrie`#ctrie]`_`f
• `F0af`_`[Concurrent modification problem`#concurrent-modification-problem]`_`f
• `F0af`_`[Modification counters`#modification-counters]`_`f
• `F0af`_`[Collections.synchronizedMap()`#collections-synchronizedmap]`_`f
• `F0af`_`[Native synchronization`#native-synchronization]`_`f
• `F0af`_`[ReentrantReadWriteLock`#reentrantreadwritelock]`_`f
• `F0af`_`[Convoys`#convoys]`_`f
• `F0af`_`[Multiple cores`#multiple-cores]`_`f
• `F0af`_`[Predictable latency`#predictable-latency]`_`f
• `F0af`_`[Weak consistency`#weak-consistency]`_`f
• `F0af`_`[ConcurrentMap 1.5 methods`#concurrentmap-1-5-methods]`_`f
• `F0af`_`[ConcurrentMap 1.8 methods`#concurrentmap-1-8-methods]`_`f
• `F0af`_`[Lock-free atomicity`#lock-free-atomicity]`_`f
• `F0af`_`[History`#history]`_`f
• `F0af`_`[See also`#see-also]`_`f
• `F0af`_`[Citations`#citations]`_`f
• `F0af`_`[References`#references]`_`f
• `F0af`_`[External links`#external-links]`_`f

-─

>>Java Map Interfaces

The version 1.8 Map interface diagram has the shape below. Sets can be considered sub-cases of corresponding Maps in which the values are always a particular constant which can be ignored, although the Set API uses corresponding but differently named methods. At the bottom is the `B100`F9d9java.util.concurrent.ConcurrentNavigableMap`f`b, which is a multiple-inheritance.

• `!`B100`F9d9java.util.Collection`f`b`!

• `!`B100`F9d9java.util.Map`f`b`!

• `!`B100`F9d9java.util.SortedMap`f`b`!

• `!`B100`F9d9java.util.NavigableMap`f`b`!

• `!`B100`F9d9java.util.concurrent.ConcurrentNavigableMap`f`b`!

• `!`B100`F9d9java.util.concurrent.ConcurrentMap`f`b`!

• `!`B100`F9d9java.util.concurrent.ConcurrentNavigableMap`f`b`!

>>Implementations

>>>ConcurrentHashMap

For unordered access as defined in the `B100`F9d9java.util.Map`f`b interface, the `B100`F9d9java.util.concurrent.ConcurrentHashMap`f`b implements `B100`F9d9java.util.concurrent.ConcurrentMap`f`b.`:cite-ref-footnotegoetzpeierlsblochbowbeer200685-86-5-2-1-concurrenthashmap-2-0[`F5bf`_`[2`#cite-note-footnotegoetzpeierlsblochbowbeer200685-86-5-2-1-concurrenthashmap-2]`_`f] The mechanism is a hash access to a hash table with lists of entries, each entry holding a key, a value, the hash, and a next reference. Previous to Java 8, there were multiple locks each serializing access to a 'segment' of the table. In Java 8, native synchronization is used on the heads of the lists themselves, and the lists can mutate into small trees when they threaten to grow too large due to unfortunate hash collisions. Also, Java 8 uses the compare-and-set primitive optimistically to place the initial heads in the table, which is very fast. Performance is O(n), but there are delays occasionally when rehashing is necessary. After the hash table expands, it never shrinks, possibly leading to a memory 'leak' after entries are removed.

>>>ConcurrentSkipListMap

For ordered access as defined by the `B100`F9d9java.util.NavigableMap`f`b interface, `B100`F9d9java.util.concurrent.ConcurrentSkipListMap`f`b was added in Java 1.6,`:cite-ref-footnotegoetzpeierlsblochbowbeer200684-85-5-2-concurrent-collections-1-1[`F5bf`_`[1`#cite-note-footnotegoetzpeierlsblochbowbeer200684-85-5-2-concurrent-collections-1]`_`f] and implements `B100`F9d9java.util.concurrent.ConcurrentMap`f`b and also `B100`F9d9java.util.concurrent.ConcurrentNavigableMap`f`b. It is a `F33f`_`[Skip list`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Skip_list]`_`f which uses Lock-free techniques to make a tree. Performance is O(log(n)).

>>>Ctrie

• Ctrie A `F33f`_`[trie`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Trie]`_`f-based Lock-free tree.

>>Concurrent modification problem

One problem solved by the Java 1.5 `B100`F9d9java.util.concurrent`f`b package is that of concurrent modification. The collection classes it provides may be reliably used by multiple Threads.

All Thread-shared non-concurrent Maps and other collections need to use some form of explicit locking such as native synchronization in order to prevent concurrent modification, or else there must be a way to prove from the program logic that concurrent modification cannot occur. Concurrent modification of a `B100`F9d9Map`f`b by multiple Threads will sometimes destroy the internal consistency of the data structures inside the `B100`F9d9Map`f`b, leading to bugs which manifest rarely or unpredictably, and which are difficult to detect and fix. Also, concurrent modification by one Thread with read access by another Thread or Threads will sometimes give unpredictable results to the reader, although the Map's internal consistency will not be destroyed. Using external program logic to prevent concurrent modification increases code complexity and creates an unpredictable risk of errors in existing and future code, although it enables non-concurrent Collections to be used. However, either locks or program logic cannot coordinate external threads which may come in contact with the `B100`F9d9Collection`f`b.

>>>Modification counters

In order to help with the concurrent modification problem, the non-concurrent `B100`F9d9Map`f`b implementations and other `B100`F9d9Collection`f`bs use internal modification counters which are consulted before and after a read to watch for changes: the writers increment the modification counters. A concurrent modification is supposed to be detected by this mechanism, throwing a `!`B100`F9d9java.util.ConcurrentModificationException`f`b`!,`:cite-ref-footnotegoetzpeierlsblochbowbeer200682-83-5-1-2-iterators-and-concurrentmodificationexception-3-0[`F5bf`_`[3`#cite-note-footnotegoetzpeierlsblochbowbeer200682-83-5-1-2-iterators-and-concurrentmodificationexception-3]`_`f] but it is not guaranteed to occur in all cases and should not be relied on. The counter maintenance is also a performance reducer. For performance reasons, the counters are not volatile, so it is not guaranteed that changes to them will be propagated between `B100`F9d9Thread`f`bs.

>>>Collections.synchronizedMap()

One solution to the concurrent modification problem is using a particular wrapper class provided by a factory in `!`B100`F9d9java.util.Collections`f`b`! : `B100`F9d9`!public static`! <K,V> Map<K,V> synchronizedMap(Map<K,V> m)`f`b which wraps an existing non-thread-safe `B100`F9d9Map`f`b with methods that synchronize on an internal mutex.`:cite-ref-footnotegoetzpeierlsblochbowbeer200684-85-5-2-1-concurrenthashmap-4-0[`F5bf`_`[4`#cite-note-footnotegoetzpeierlsblochbowbeer200684-85-5-2-1-concurrenthashmap-4]`_`f] There are also wrappers for the other kinds of Collections. This is a partial solution, because it is still possible that the underlying `B100`F9d9Map`f`b can be inadvertently accessed by `B100`F9d9Thread`f`bs which keep or obtain unwrapped references. Also, all Collections implement the `!`B100`F9d9java.lang.Iterable`f`b`! but the synchronized-wrapped Maps and other wrapped `B100`F9d9Collections`f`b do not provide synchronized iterators, so the synchronization is left to the client code, which is slow and error prone and not possible to expect to be duplicated by other consumers of the synchronized `B100`F9d9Map`f`b. The entire duration of the iteration must be protected as well. Furthermore, a `B100`F9d9Map`f`b which is wrapped twice in different places will have different internal mutex Objects on which the synchronizations operate, allowing overlap. The delegation is a performance reducer, but modern Just-in-Time compilers often inline heavily, limiting the performance reduction. Here is how the wrapping works inside the wrapper - the mutex is just a final `B100`F9d9Object`f`b and m is the final wrapped `B100`F9d9Map`f`b:

`B100`F9d9 public V put(K key, V value) {`f`b
`B100`F9d9 synchronized (mutex) {`f`b
`B100`F9d9 return m.put(key, value);`f`b
`B100`F9d9 }`f`b
`B100`F9d9 }`f`b

The synchronization of the iteration is recommended as follows; however, this synchronizes on the wrapper rather than on the internal mutex, allowing overlap:`:cite-ref-5[`F5bf`_`[5`#cite-note-5]`_`f]

`B100`F9d9 Map<String, String> wrappedMap = Collections.synchronizedMap(map);`f`b
`B100`F9d9 ...`f`b
`B100`F9d9 synchronized (wrappedMap) {`f`b
`B100`F9d9 for (final String s : wrappedMap.keySet()) {`f`b
`B100`F9d9 // some possibly long operation executed possibly`f`b
`B100`F9d9 // many times, delaying all other accesses`f`b
`B100`F9d9 }`f`b
`B100`F9d9 }`f`b

>>>Native synchronization

Any `B100`F9d9Map`f`b can be used safely in a multi-threaded system by ensuring that all accesses to it are handled by the Java synchronization mechanism:

`B100`F9d9 final Map<String, String> map = new HashMap<>();`f`b
`B100`F9d9 ...`f`b
`B100`F9d9 // Thread A`f`b
`B100`F9d9 // Use the map itself as the lock. Any agreed object can be used instead.`f`b
`B100`F9d9 synchronized(map) {`f`b
`B100`F9d9 map.put("key","value");`f`b
`B100`F9d9 }`f`b
`B100`F9d9 ..`f`b
`B100`F9d9 // Thread B`f`b
`B100`F9d9 synchronized (map) {`f`b
`B100`F9d9 String result = map.get("key");`f`b
`B100`F9d9 ...`f`b
`B100`F9d9 }`f`b
`B100`F9d9 ...`f`b
`B100`F9d9 // Thread C`f`b
`B100`F9d9 synchronized (map) {`f`b
`B100`F9d9 for (final Entry<String, String> s : map.entrySet()) {`f`b
`B100`F9d9 /*`f`b
`B100`F9d9 * Some possibly slow operation, delaying all other supposedly fast operations.`f`b
`B100`F9d9 * Synchronization on individual iterations is not possible.`f`b
`B100`F9d9 */`f`b
`B100`F9d9 ...`f`b
`B100`F9d9 }`f`b
`B100`F9d9 }`f`b

>>>ReentrantReadWriteLock

The code using a `!`B100`F9d9java.util.concurrent.ReentrantReadWriteLock`f`b`! is similar to that for native synchronization. However, for safety, the locks should be used in a try/finally block so that early exit such as `B100`F9d9java.lang.Exception`f`b throwing or break/continue will be sure to pass through the unlock. This technique is better than using synchronization`:cite-ref-footnotegoetzpeierlsblochbowbeer200695-98-13-5-read-write-locks-6-0[`F5bf`_`[6`#cite-note-footnotegoetzpeierlsblochbowbeer200695-98-13-5-read-write-locks-6]`_`f] because reads can overlap each other, there is a new issue in deciding how to prioritize the writes with respect to the reads. For simplicity a `!`B100`F9d9java.util.concurrent.ReentrantLock`f`b`! can be used instead, which makes no read/write distinction. More operations on the locks are possible than with synchronization, such as `B100`F9d9tryLock()`f`b and `B100`F9d9tryLock(long timeout, TimeUnit unit)`f`b.

`B100`F9d9 final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();`f`b
`B100`F9d9 final ReadLock readLock = lock.readLock();`f`b
`B100`F9d9 final WriteLock writeLock = lock.writeLock();`f`b
`B100`F9d9 ..`f`b
`B100`F9d9 // Thread A`f`b
`B100`F9d9 try {`f`b
`B100`F9d9 writeLock.lock();`f`b
`B100`F9d9 map.put("key","value");`f`b
`B100`F9d9 ...`f`b
`B100`F9d9 } finally {`f`b
`B100`F9d9 writeLock.unlock();`f`b
`B100`F9d9 }`f`b
`B100`F9d9 ...`f`b
`B100`F9d9 // Thread B`f`b
`B100`F9d9 try {`f`b
`B100`F9d9 readLock.lock();`f`b
`B100`F9d9 final String s = map.get("key");`f`b
`B100`F9d9 ..`f`b
`B100`F9d9 } finally {`f`b
`B100`F9d9 readLock.unlock();`f`b
`B100`F9d9 }`f`b
`B100`F9d9 // Thread C`f`b
`B100`F9d9 try {`f`b
`B100`F9d9 readLock.lock();`f`b
`B100`F9d9 for (final Entry<String, String> s : map.entrySet()) {`f`b
`B100`F9d9 /*`f`b
`B100`F9d9 * Some possibly slow operation, delaying all other supposedly fast operations.`f`b
`B100`F9d9 * Synchronization on individual iterations is not possible.`f`b
`B100`F9d9 */`f`b
`B100`F9d9 ...`f`b
`B100`F9d9 }`f`b
`B100`F9d9 } finally {`f`b
`B100`F9d9 readLock.unlock();`f`b
`B100`F9d9 }`f`b

>>Convoys

Mutual exclusion has a `F33f`_`[lock convoy`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Lock_convoy]`_`f problem, in which threads may pile up on a lock, causing the JVM to need to maintain expensive queues of waiters and to 'park' the waiting `B100`F9d9Thread`f`bs. It is expensive to park and unpark a `B100`F9d9Thread`f`bs, and a slow context switch may occur. Context switches require from microseconds to milliseconds, while the Map's own basic operations normally take nanoseconds. Performance can drop to a small fraction of a single `B100`F9d9Thread`f`b's throughput as contention increases. When there is no or little contention for the lock, there is little performance impact; however, except for the lock's contention test. Modern JVMs will inline most of the lock code, reducing it to only a few instructions, keeping the no-contention case very fast. Reentrant techniques like native synchronization or `B100`F9d9java.util.concurrent.ReentrantReadWriteLock`f`b however have extra performance-reducing baggage in the maintenance of the reentrancy depth, affecting the no-contention case as well. The Convoy problem seems to be easing with modern JVMs, but it can be hidden by slow context switching: in this case, latency will increase, but throughput will continue to be acceptable. With hundreds of `B100`F9d9Thread`f`bs , a context switch time of 10ms produces a latency in seconds.

>>Multiple cores

Mutual exclusion solutions fail to take advantage of all of the computing power of a multiple-core system, because only one `B100`F9d9Thread`f`b is allowed inside the `B100`F9d9Map`f`b code at a time. The implementations of the particular concurrent Maps provided by the Java Collections Framework and others sometimes take advantage of multiple cores using `F33f`_`[lock free`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Lock_free]`_`f programming techniques. Lock-free techniques use operations like the compareAndSet() intrinsic method available on many of the Java classes such as `!`B100`F9d9AtomicReference`f`b`! to do conditional updates of some Map-internal structures atomically. The compareAndSet() primitive is augmented in the JCF classes by native code that can do compareAndSet on special internal parts of some Objects for some algorithms (using 'unsafe' access). The techniques are complex, relying often on the rules of inter-thread communication provided by volatile variables, the happens-before relation, special kinds of lock-free 'retry loops' (which are not like spin locks in that they always produce progress). The compareAndSet() relies on special processor-specific instructions. It is possible for any Java code to use for other purposes the compareAndSet() method on various concurrent classes to achieve Lock-free or even Wait-free concurrency, which provides finite latency. Lock-free techniques are simple in many common cases and with some simple collections like stacks.

The diagram indicates how synchronizing using `B100`F9d9Collections.synchronizedMap(java.util.Map)`f`bwrapping a regular HashMap (purple) may not scale as well as ConcurrentHashMap (red). The others are the ordered ConcurrentNavigableMaps AirConcurrentMap (blue) and ConcurrentSkipListMap (CSLM green). (The flat spots may be rehashes producing tables that are bigger than the Nursery, and ConcurrentHashMap takes more space. Note y axis should say 'puts K'. System is 8-core i7 2.5 GHz, with -Xms5000m to prevent GC). GC and JVM process expansion change the curves considerably, and some internal lock-Free techniques generate garbage on contention.

>>Predictable latency

Yet another problem with mutual exclusion approaches is that the assumption of complete atomicity made by some single-threaded code creates sporadic unacceptably long inter-Thread delays in a concurrent environment. In particular, Iterators and bulk operations like putAll() and others can take a length of time proportional to the Map size, delaying other `B100`F9d9Thread`f`bs that expect predictably low latency for non-bulk operations. For example, a multi-threaded web server cannot allow some responses to be delayed by long-running iterations of other threads executing other requests that are searching for a particular value. Related to this is the fact that `B100`F9d9Thread`f`bs that lock the `B100`F9d9Map`f`b do not actually have any requirement ever to relinquish the lock, and an infinite loop in the owner `B100`F9d9Thread`f`b may propagate permanent blocking to other `B100`F9d9Thread`f`bs . Slow owner `B100`F9d9Thread`f`bs can sometimes be Interrupted. Hash-based Maps also are subject to spontaneous delays during rehashing.

>>Weak consistency

The `B100`F9d9java.util.concurrent`f`b packages' solution to the concurrent modification problem, the convoy problem, the predictable latency problem, and the multi-core problem includes an architectural choice called weak consistency. This choice means that reads like `B100`F9d9get(java.lang.Object)`f`b will not block even when updates are in progress, and it is allowable even for updates to overlap with themselves and with reads. Weak consistency allows, for example, the contents of a `B100`F9d9ConcurrentMap`f`b to change during an iteration of it by a single `B100`F9d9Thread`f`b.`:cite-ref-footnotegoetzpeierlsblochbowbeer200685-86-5-21-concurrenthashmap-7-0[`F5bf`_`[7`#cite-note-footnotegoetzpeierlsblochbowbeer200685-86-5-21-concurrenthashmap-7]`_`f] The Iterators are designed to be used by one `B100`F9d9Thread`f`b at a time. So, for example, a `B100`F9d9Map`f`b containing two entries that are inter-dependent may be seen in an inconsistent way by a reader `B100`F9d9Thread`f`b during modification by another `B100`F9d9Thread`f`b. An update that is supposed to change the key of an Entry (`*k1,v`*) to an Entry (`*k2,v`*) atomically would need to do a remove(`*k1`*) and then a put(`*k2, v`*), while an iteration might miss the entry or see it in two places. Retrievals return the value for a given key that reflects `*the latest previous completed`* update for that key. Thus there is a 'happens-before' relation.

There is no way for `B100`F9d9ConcurrentMap`f`bs to lock the entire table. There is no possibility of `B100`F9d9ConcurrentModificationException`f`b as there is with inadvertent concurrent modification of non-concurrent `B100`F9d9Map`f`bs. The `B100`F9d9size()`f`b method may take a long time, as opposed to the corresponding non-concurrent `B100`F9d9Map`f`bs and other collections which usually include a size field for fast access, because they may need to scan the entire `B100`F9d9Map`f`b in some way. When concurrent modifications are occurring, the results reflect the state of the `B100`F9d9Map`f`b at some time, but not necessarily a single consistent state, hence `B100`F9d9size()`f`b, `B100`F9d9isEmpty()`f`b and `B100`F9d9containsValue(java.lang.Object)`f`b may be best used only for monitoring.

>>ConcurrentMap 1.5 methods

There are some operations provided by `B100`F9d9ConcurrentMap`f`b that are not in `B100`F9d9Map`f`b - which it extends - to allow atomicity of modifications. The replace(`*K, v1, v2`*) will test for the existence of `*v1`* in the Entry identified by `*K`* and only if found, then the `*v1`* is replaced by `*v2`* atomically. The new replace(`*k,v`*) will do a put(`*k,v`*) only if `*k`* is already in the Map. Also, putIfAbsent(`*k,v`*) will do a put(`*k,v`*) only if `*k`* is not already in the `B100`F9d9Map`f`b, and remove(k, v) will remove the Entry for v only if v is present. This atomicity can be important for some multi-threaded use cases, but is not related to the weak-consistency constraint.

For `B100`F9d9ConcurrentMap`f`bs, the following are atomic.

m.putIfAbsent(k, v) is atomic but equivalent to:

`B100`F9d9 if (k == null || v == null)`f`b
`B100`F9d9 throw new NullPointerException();`f`b
`B100`F9d9 if (!m.containsKey(k)) {`f`b
`B100`F9d9 return m.put(k, v);`f`b
`B100`F9d9 } else {`f`b
`B100`F9d9 return m.get(k);`f`b
`B100`F9d9 }`f`b

m.replace(k, v) is atomic but equivalent to:

`B100`F9d9 if (k == null || v == null)`f`b
`B100`F9d9 throw new NullPointerException();`f`b
`B100`F9d9 if (m.containsKey(k)) {`f`b
`B100`F9d9 return m.put(k, v);`f`b
`B100`F9d9 } else {`f`b
`B100`F9d9 return null;`f`b
`B100`F9d9 }`f`b

m.replace(k, v1, v2) is atomic but equivalent to:

`B100`F9d9 if (k == null || v1 == null || v2 == null)`f`b
`B100`F9d9 throw new NullPointerException();`f`b
`B100`F9d9 if (m.containsKey(k) && Objects.equals(m.get(k), v1)) {`f`b
`B100`F9d9 m.put(k, v2);`f`b
`B100`F9d9 return true;`f`b
`B100`F9d9 } else`f`b
`B100`F9d9 return false;`f`b
`B100`F9d9 }`f`b

m.remove(k, v) is atomic but equivalent to:

`B100`F9d9 // if Map does not support null keys or values (apparently independently)`f`b
`B100`F9d9 if (k == null || v == null)`f`b
`B100`F9d9 throw new NullPointerException();`f`b
`B100`F9d9 if (m.containsKey(k) && Objects.equals(m.get(k), v)) {`f`b
`B100`F9d9 m.remove(k);`f`b
`B100`F9d9 return true;`f`b
`B100`F9d9 } else`f`b
`B100`F9d9 return false;`f`b
`B100`F9d9 }`f`b

>>ConcurrentMap 1.8 methods

Because `B100`F9d9Map`f`b and `B100`F9d9ConcurrentMap`f`b are interfaces, new methods cannot be added to them without breaking implementations. However, Java 1.8 added the capability for default interface implementations and it added to the `B100`F9d9Map`f`b interface default implementations of some new methods getOrDefault(Object, V), forEach(BiConsumer), replaceAll(BiFunction), computeIfAbsent(K, Function), computeIfPresent(K, BiFunction), compute(K,BiFunction), and merge(K, V, BiFunction). The default implementations in `B100`F9d9Map`f`b do not guarantee atomicity, but in the `B100`F9d9ConcurrentMap`f`b overriding defaults these use `F33f`_`[Lock free`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Lock_free]`_`f techniques to achieve atomicity, and existing ConcurrentMap implementations will automatically be atomic. The lock-free techniques may be slower than overrides in the concrete classes, so concrete classes may choose to implement them atomically or not and document the concurrency properties.

>>Lock-free atomicity

It is possible to use `F33f`_`[lock-free`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Lock-free]`_`f techniques with ConcurrentMaps because they include methods of a `F33f`_`[sufficiently high consensus number, namely infinity`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Consensus_(computer_science)]`_`f, meaning that any number of `B100`F9d9Thread`f`bs may be coordinated. This example could be implemented with the Java 8 merge() but it shows the overall lock-free pattern, which is more general. This example is not related to the internals of the ConcurrentMap but to the client code's use of the ConcurrentMap. For example, if we want to multiply a value in the Map by a constant C atomically:

`B100`F9d9 static final long C = 10;`f`b
`B100`F9d9 void atomicMultiply(ConcurrentMap<Long, Long> map, Long key) {`f`b
`B100`F9d9 for (;;) {`f`b
`B100`F9d9 Long oldValue = map.get(key);`f`b
`B100`F9d9 // Assuming oldValue is not null. This is the 'payload' operation, and should not have side-effects due to possible re-calculation on conflict`f`b
`B100`F9d9 Long newValue = oldValue * C;`f`b
`B100`F9d9 if (map.replace(key, oldValue, newValue))`f`b
`B100`F9d9 break;`f`b
`B100`F9d9 }`f`b
`B100`F9d9 }`f`b

The putIfAbsent(`*k, v`*) is also useful when the entry for the key is allowed to be absent. This example could be implemented with the Java 8 compute() but it shows the overall lock-free pattern, which is more general. The replace(`*k,v1,v2`*) does not accept null parameters, so sometimes a combination of them is necessary. In other words, if `*v1`* is null, then putIfAbsent(`*k, v2`*) is invoked, otherwise replace(`*k,v1,v2`*) is invoked.

`B100`F9d9 void atomicMultiplyNullable(ConcurrentMap<Long, Long> map, Long key) {`f`b
`B100`F9d9 for (;;) {`f`b
`B100`F9d9 Long oldValue = map.get(key);`f`b
`B100`F9d9 // This is the 'payload' operation, and should not have side-effects due to possible re-calculation on conflict`f`b
`B100`F9d9 Long newValue = oldValue == null ? INITIAL_VALUE : oldValue * C;`f`b
`B100`F9d9 if (replaceNullable(map, key, oldValue, newValue))`f`b
`B100`F9d9 break;`f`b
`B100`F9d9 }`f`b
`B100`F9d9 }`f`b
`B100`F9d9 ...`f`b
`B100`F9d9 static boolean replaceNullable(ConcurrentMap<Long, Long> map, Long key, Long v1, Long v2) {`f`b
`B100`F9d9 return v1 == null ? map.putIfAbsent(key, v2) == null : map.replace(key, v1, v2);`f`b
`B100`F9d9 }`f`b

>>History

The Java collections framework was designed and developed primarily by `F33f`_`[Joshua Bloch`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Joshua_Bloch]`_`f, and was introduced in `F33f`_`[JDK 1.2`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=J2SE_1.2]`_`f.`:cite-ref-8[`F5bf`_`[8`#cite-note-8]`_`f] The original concurrency classes came from `F33f`_`[Doug Lea`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Doug_Lea]`_`f's `:cite-ref-douglea2-9-0[`F5bf`_`[9`#cite-note-douglea2-9]`_`f] collection package.

>>See also

• `F33f`_`[Java Collections Framework`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Java_Collections_Framework]`_`f
• `F33f`_`[Container (data structure)`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Container_(data_structure)]`_`f
• `F33f`_`[Java concurrency`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Java_concurrency]`_`f
• `F33f`_`[Lock free`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Lock_free]`_`f

>>Citations

`:cite-note-footnotegoetzpeierlsblochbowbeer200684-85-5-2-concurrent-collections-1`!1.`! `F0af`_`[↑`#cite-ref-footnotegoetzpeierlsblochbowbeer200684-85-5-2-concurrent-collections-1-0]`_`f `F33f`_`[Goetz et al. 2006`#citerefgoetzpeierlsblochbowbeer2006]`_`f, pp. 84–85, §5.2 Concurrent collections.
`:cite-note-footnotegoetzpeierlsblochbowbeer200685-86-5-2-1-concurrenthashmap-2`!2.`! `F0af`_`[↑`#cite-ref-footnotegoetzpeierlsblochbowbeer200685-86-5-2-1-concurrenthashmap-2-0]`_`f `F33f`_`[Goetz et al. 2006`#citerefgoetzpeierlsblochbowbeer2006]`_`f, pp. 85–86, §5.2.1 ConcurrentHashMap.
`:cite-note-footnotegoetzpeierlsblochbowbeer200682-83-5-1-2-iterators-and-concurrentmodificationexception-3`!3.`! `F0af`_`[↑`#cite-ref-footnotegoetzpeierlsblochbowbeer200682-83-5-1-2-iterators-and-concurrentmodificationexception-3-0]`_`f `F33f`_`[Goetz et al. 2006`#citerefgoetzpeierlsblochbowbeer2006]`_`f, pp. 82–83, §5.1.2 Iterators and ConcurrentModificationException.
`:cite-note-footnotegoetzpeierlsblochbowbeer200684-85-5-2-1-concurrenthashmap-4`!4.`! `F0af`_`[↑`#cite-ref-footnotegoetzpeierlsblochbowbeer200684-85-5-2-1-concurrenthashmap-4-0]`_`f `F33f`_`[Goetz et al. 2006`#citerefgoetzpeierlsblochbowbeer2006]`_`f, pp. 84–85, §5.2.1 ConcurrentHashMap.
`:cite-note-5`!5.`! `F0af`_`[↑`#cite-ref-5]`_`f "java.util.Collections.synchronizedMap". Java / Java SE / 11 / API / java.base. `*Oracle Help Center`*. September 19, 2018. Retrieved 2020-07-17.
`:cite-note-footnotegoetzpeierlsblochbowbeer200695-98-13-5-read-write-locks-6`!6.`! `F0af`_`[↑`#cite-ref-footnotegoetzpeierlsblochbowbeer200695-98-13-5-read-write-locks-6-0]`_`f `F33f`_`[Goetz et al. 2006`#citerefgoetzpeierlsblochbowbeer2006]`_`f, pp. 95–98, §13.5 Read-write locks.
`:cite-note-footnotegoetzpeierlsblochbowbeer200685-86-5-21-concurrenthashmap-7`!7.`! `F0af`_`[↑`#cite-ref-footnotegoetzpeierlsblochbowbeer200685-86-5-21-concurrenthashmap-7-0]`_`f `F33f`_`[Goetz et al. 2006`#citerefgoetzpeierlsblochbowbeer2006]`_`f, pp. 85–86, §5.21 ConcurrentHashMap.
`:cite-note-8`!8.`! `F0af`_`[↑`#cite-ref-8]`_`f `:citerefvanhelsuw-1999`aVanhelsuwé, Laurence (January 1, 1999). "The battle of the container frameworks: which should you use?". `*`F33f`_`[JavaWorld`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=JavaWorld]`_`f`*. Retrieved 2020-07-17.
`:cite-note-douglea2-9`!9.`! `F0af`_`[↑`#cite-ref-douglea2-9-0]`_`f `:citereflea`a`F33f`_`[Lea, Doug`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Doug_Lea]`_`f. "Overview of package util.concurrent Release 1.3.4". Retrieved 2011-01-01.

>>References

• `:citerefgoetzpeierlsblochbowbeer2006`aGoetz, Brian; Peierls, Tim; Bloch, Joshua; Bowbeer, Joseph; Holmes, David; Lea, Doug (2006). `*Java Concurrency in Practice`*. Addison Wesley. `F33f`_`[ISBN`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=ISBN_(identifier)]`_`f 0-321-34960-1. `F33f`_`[OL`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=OL_(identifier)]`_`f 25208908M.
• `:citereflea1999`aLea, Doug (1999). `*Concurrent Programming in Java: Design Principles and Patterns`*. Addison Wesley. `F33f`_`[ISBN`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=ISBN_(identifier)]`_`f 0-201-31009-0. `F33f`_`[OL`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=OL_(identifier)]`_`f 55044M.

>>External links

The Wikibook

Java Programming

has a page on the topic of:

Collections

• Collections Lessons
• Java 6 Collection Tutorial — By Jakob Jenkov, Kadafi Kamphulusa
• Taming Tiger: The Collections Framework
• 'The Collections Framework' (Oracle Java SE 8 documentation)
• 'The Java Tutorials - Collections' by Josh Bloch
• What Java Collection should I use? — A handy flowchart to simplify selection of collections
• 'Which Java Collection to use?' — by Janeve George

`c`F0af`_`[↑ Back to top`#top]`_`f`a